/**
 * Licensed under the Apache License Version 2.0.
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package net.interknit.knitter.cg.model;

import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import net.interknit.knitter.cg.util.CGLogger;
import net.interknit.knitter.cg.util.Utils;

/**
 *
 */
public final class JavaTypeMap
{
	private static Map<String, JavaType> javaTypes = new ConcurrentHashMap<String, JavaType>();
	private static boolean complete;

	static void complete()
	{
		complete = true;
	}

	static void begin()
	{
		javaTypes.clear();
		complete = false;
	}

	static boolean put(String javaTypeId, JavaType javaType)
	{
		if(complete)
			throw new RuntimeException("JavaTypeMap is read-only, new values cannot be added");

		if(javaTypes.containsKey(javaTypeId))
		{
			return false;
		}
		else
		{
			javaTypes.put(javaTypeId, javaType);
			return true;
		}
	}

	static boolean isEmpty()
	{
		return javaTypes.isEmpty();
	}

	static Map<String, JavaType> map()
	{
		return javaTypes;
	}

	static int size()
	{
		return javaTypes.size();
	}

	public static Set<String> keys()
	{
		return javaTypes.keySet();
	}

	public static boolean containsKey(String javaTypeId)
	{
		return javaTypes.containsKey(javaTypeId);
	}

	public static JavaType get(String javaTypeId)
	{
		return javaTypes.get(javaTypeId);
	}

	static void printResult(String typeId, long time)
	throws IOException
	{
		for(String javaTypeId : JavaTypeMap.keys())
		{
			CGLogger.progressLog("===============================================================================");
			CGLogger.progressLog("JavaType: " + javaTypeId + " [" + JavaTypeMap.get(javaTypeId).getConcreteJavaType() + "]");
			CGLogger.progressLog("===============================================================================");
			CGLogger.progressLog("JavaMethods:");
			CGLogger.progressLog(Utils.displayPrettyCollection(JavaTypeMap.get(javaTypeId).getJavaMethods()));
			CGLogger.progressLog("-------------------------------------------------------------------------------");
		}

		CGLogger.progressLog("Scanned JavaTypes: ");
		CGLogger.progressLog(Utils.displayPrettyMap(javaTypes));

		CGLogger.progressLog("(total " + JavaTypeMap.size() + " JavaTypes created in " + time + " milliseconds)");
		CGLogger.progressLog("###############################################################################");
	}
}
